export const ajaxMethod = (method = "GET", url, params) => {
    const request = window.XMLHttpRequest ? new XMLHttpRequest() : null;
    if (!request) return Promise.reject("不支持 XMLHttpRequest ");
    return new Promise(function (resolve, reject) {
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                if (request.status === 200) {
                    resolve(JSON.parse(request.response));
                } else {
                    reject(request.status);
                }
            }
        };
        if (method.toUpperCase() === "GET") {
            const arr = [];
            for (let key in params) {
                arr.push(key + "=" + params[key]);
            }
            const getData = arr.join("&");
            request.open("GET", url + "?" + getData, true);
            request.send(null);
        } else if (method.toUpperCase() === "POST") {
            request.open("POST", url, true);
            request.responseType = "json";
            request.setRequestHeader(
                "Content-Type",
                "application/x-www-form-urlencoded;charset=utf-8"
            );
            request.send(params);
        }
    });
};
